package b2.scene.fight
{
	import b2.config.*;
	import b2.net.*;
	import b2.scene.guide.*;
	
	import org.flixel.*;

	public class DropItemSprite extends FlxSprite
	{
		public var ITEM_WIDTH:uint = 75;
		public var itemId:int;
		public var itemIndex:int;
		public var side:int;
		//掉落物品索引
		public var objectIndex:int;
		public var basicLevel:int;
		public var strengthenLevel:int;
		public var objectNum:int;
		//倒计时
		private var countDown:int;
		private var timer:FlxTimer;
		
		public function DropItemSprite(itemId:int, itemIndex:int, side:int)
		{
			super();
			this.itemId = itemId;
			this.itemIndex = itemIndex;
			this.side = side;
			//道具
			var itemConfig:DropItemConfig = Config.dropItemConfigs[itemIndex];
			var IconImage:Class = ResourceHelper.getInstance().getClassByName(itemConfig.iconImage);
			ExternalImage.setData(IconImage, itemConfig.iconImage, ITEM_WIDTH * 3, ITEM_WIDTH);
			loadGraphic(ExternalImage, true, true, 75);
			var playerState:PlayState = FlxG.state as PlayState;
			//如果是本方势力掉落的宝物，用透明度表示其不可捡起
			if (side == playerState.mySide)
				this.alpha = 0.5;
			countDown = itemConfig.duration;
			this.addAnimation("blink", [0, 2], 4);
			this.scale.x = PlayState.TILE_WIDTH / ITEM_WIDTH;
			this.scale.y = PlayState.TILE_WIDTH / ITEM_WIDTH;
			this.width = PlayState.TILE_WIDTH;
			this.height = PlayState.TILE_WIDTH;
			this.offset.x = (ITEM_WIDTH - PlayState.TILE_WIDTH) / 2;
			this.offset.y = (ITEM_WIDTH - PlayState.TILE_WIDTH) / 2;
			
			timer = new FlxTimer();
			timer.start(1, 999999, onTick);
		}
		
		private function onTick(timer:FlxTimer):void
		{
			if (!this.alive || GuideScene.getInstance().enabled)
				return;
			if (countDown > 0)
			{
				countDown--;
				if (countDown == 5)
					play("blink");
			}
			else
			{
				kill();
				destroy();
			}
		}
		
		public override function destroy():void
		{
			super.destroy();
			timer.destroy();
		}
		
	}
}